package client.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import client.io.ClientIO;
import domain.action.Action;
import domain.doctor.Doctor;
import domain.patient.Patient;
import domain.session.Request;
import domain.session.Response;
import domain.user.SystemUser;

public class DoctorService {

	public static SystemUser getDoctor(Integer userId) {
		JSONObject requestJson = new JSONObject();
		requestJson.put("userId", userId);
		ClientIO.sendRequest(new Request(Action.GET_DOCTOR, requestJson));

		Response response = ClientIO.getResponse();
		return new Patient(response.getJSON());
	}

	public static List<Doctor> getAllDoctors() {
		JSONObject requestJson = new JSONObject();
		ClientIO.sendRequest(new Request(Action.GET_ALL_DOCTORS, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
		JSONArray doctorArray = responseJson.getJSONArray("doctors");
		
		List<Doctor> doctors = new ArrayList<Doctor>();
		
		for (int i = 0; i < doctorArray.length(); i++) {
			doctors.add(new Doctor(doctorArray.getJSONObject(i)));
		}
		
		return doctors;
	}

}
